﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using DB;
using System.Web.SessionState;


/// <summary>
///LoginChecker 的摘要说明
/// </summary>
public class LoginChecker : System.Web.SessionState.IRequiresSessionState
{
    public LoginChecker()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static string checkUser(string username, string password, string rolename, HttpContext context)
    {
        string sql_str = "exec login_checkUser " + username.Trim() + "," + password.Trim() + "," + rolename.Trim();
        string data_back = "";
        DataAccess da = new DataAccess();
        DataTable dt = da.GetTable(sql_str);
        if (dt.Rows.Count > 0)
        {
            string nickname = (string)dt.Rows[0]["nickname"];
            int roleid = (int)dt.Rows[0]["roleid"];
            context.Session["checkedflag"] = "yes";
            context.Session["roleid"] = roleid;
            context.Session["nickname"] = nickname;
            data_back = "{\"checked\":\"yes\",\"nickname\":\"" + nickname + "\",\"roleid\":\"" + roleid + "\"}";
        }
        else
        {
            context.Session["checkedflag"] = "no";
            data_back = "{\"checked\":\"no\"}";
        }
        return data_back;
    }

    public static string getUserFromSession()
    {
        string data_back = "";
        data_back = "{\"checked\":\"" + HttpContext.Current.Session["checkedflag"] + "\",\"nickname\":\"" + HttpContext.Current.Session["nickname"] + "\",\"roleid\":\"" + HttpContext.Current.Session["roleid"] + "\"}";
        return data_back;
    }
}